package com.example.androidlearning2200;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

import com.example.androidlearning2200.adapter.ContactAdapter;
import com.example.androidlearning2200.bean.ContactBean;

import java.util.ArrayList;
import java.util.List;

public class ContactActivity extends AppCompatActivity {

    private final int REQ_PERMISSION_CODE = 100;
    private RecyclerView rvContact;
    private List<ContactBean> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);
        rvContact = findViewById(R.id.rvContact);
        rvContact.setLayoutManager(new LinearLayoutManager(this));
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            // 目前App未被授予读取联系人的权限，需要主动申请
            // 申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, REQ_PERMISSION_CODE);
        } else {
            readContacts();
        }
    }

    /**
     * 申请权限的回调
     *
     * @param requestCode  The request code passed in {@link #requestPermissions(
     *android.app.Activity, String[], int)}
     * @param permissions  The requested permissions. Never null.
     * @param grantResults The grant results for the corresponding permissions
     *                     which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
     *                     or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQ_PERMISSION_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户同意授权
                // 得到权限，可以调用读取联系人的方法
                readContacts();
            } else {
                // 用户拒绝授权
                Toast.makeText(this, "用户拒绝授予读取联系人的权限", Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * 读取联系人
     * 需要用到ContentResolver和Cursor
     */
    private void readContacts() {
        ContentResolver contentResolver = getContentResolver();
        String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        Cursor cursor = contentResolver.query(contactUri, projection, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            String phone = cursor.getString(1);
            Log.d("Contacts", name + phone);
            datas.add(new ContactBean(name, phone));
        }
        cursor.close();
        rvContact.setAdapter(new ContactAdapter(datas));
    }

}